//
//  HomeViewController.swift
//  SwiftSina
//
//  Created by 七 on 2017/4/14.
//  Copyright © 2017年 Apple. All rights reserved.
//

import UIKit

class HomeViewController: BaseTableViewController {
    //记录当前是否展开
    var isPresent = false
    override func viewDidLoad() {
        super.viewDidLoad()
        if !userLogin{
            visitV?.setupVisitorViewInfo(isHome: true, imageName: "visitordiscover_feed_image_house", message: "哈拉海卡拉葫芦姐妹")
        }
        //初始化导航条
        setupNav()
    }
    func setupNav() {
        //初始化左右按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendattention", target: self, action: #selector(leftBarButtonClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(rightBarButtonClick))
        //初始化标题按钮
        let titleBtn = TitleButton()
        titleBtn.addTarget(self, action: #selector(titleButtonClick(button:)), for: .touchUpInside)
        
        navigationItem.titleView = titleBtn
    }
   
}
//NMARK:-点击事件
extension HomeViewController{
    @objc func leftBarButtonClick() {
        
    }
    @objc func rightBarButtonClick() {
        let sb = UIStoryboard(name: "QRCodeViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        present(vc!, animated: true, completion: nil)
    }
    @objc func titleButtonClick(button: UIButton) {
        button.isSelected = !button.isSelected
        let sb = UIStoryboard(name: "PopoverView", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        //设置转场代理
        //默认情况下modal会移除以前的控制器的view，替换为当前的view
        //如果自定义转场 就不会移除以前控制器的view
        vc?.transitioningDelegate = self
        vc?.modalPresentationStyle = .custom
        present(vc!, animated: true, completion: nil)
    }
}
//MARK:- 转场动画代理
extension HomeViewController: UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning{
    
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return PopOverViewController(presentedViewController: presented, presenting: presenting)
    }
    //只要实现以下方法，系统默认的动画就没有了需要自己实现
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        //拿到展现视图
//        let toVC = transitionContext.viewController(forKey: .to)
//        let fromVC = transitionContext.viewController(forKey: .from)
        if isPresent {
            
            let toView = transitionContext.view(forKey: .to)
            toView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
            //一定要将视图添加到容器上
            transitionContext.containerView.addSubview(toView!)
            //设置锚点
            toView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            //执行动画
            UIView.animate(withDuration: 0.5, animations: {
                //清空transform
                toView?.transform = CGAffineTransform.identity
            }) { (_) in
                //动画执行完毕，一定要告诉系统
                //如果不写，可能导致一些未知的错误
                transitionContext.completeTransition(true)
            }
        }else{
            //关闭
            let fromView = transitionContext.view(forKey: .from)
            UIView.animate(withDuration: 0.2, animations: {
                //注意：CGFloat是不准确的，如果写0.0会没有动画
                //压扁
                fromView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.01)
            }, completion: { (_) in
                transitionContext.completeTransition(true)
            })
        }
    }
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
}
